<template>
  <div class="app-container">
    <!-- 搜索卡片 -->
    <el-card shadow="hover" class="search-card">
      <div slot="header" class="clearfix">
        <span class="card-header-title"><i class="el-icon-search"></i> 学生信息查询</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="toggleSearch">
          {{ showSearch ? '隐藏搜索' : '显示搜索' }}
        </el-button>
      </div>
      <el-collapse-transition>
        <el-form v-show="showSearch" :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
          <el-form-item label="姓名" prop="studentName">
            <el-input
              v-model="queryParams.studentName"
              placeholder="请输入学生姓名"
              clearable
              prefix-icon="el-icon-user"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="学号" prop="number">
            <el-input
              v-model="queryParams.number"
              placeholder="请输入学生学号"
              clearable
              prefix-icon="el-icon-tickets"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="班级" prop="className">
            <el-input
              v-model="queryParams.className"
              placeholder="请输入班级名称或年级"
              clearable
              prefix-icon="el-icon-search"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item class="form-buttons">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-collapse-transition>
    </el-card>

    <!-- 操作按钮卡片 -->
    <el-card shadow="hover" class="operation-card">
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['student:student:add']"
          >新增学生</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>

    <!-- 数据表格卡片 -->
    <el-card shadow="hover" class="table-card">
      <el-table 
        v-loading="loading" 
        :data="studentList" 
        @selection-change="handleSelectionChange"
        stripe
        style="width: 100%"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" width="80" align="center">
          <template slot-scope="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="studentName" min-width="120">
          <template slot-scope="scope">
            <span class="student-name">{{ scope.row.studentName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="学号" align="center" prop="number" min-width="150" />
        <el-table-column label="班级" align="center" prop="className" min-width="150" />
        <el-table-column label="操作" align="center" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['student:student:edit']"
              class="operation-btn"
            ></el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['student:student:remove']"
              class="operation-btn delete-btn"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        class="pagination-container"
      />
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog 
      :title="title" 
      :visible.sync="open" 
      width="500px" 
      append-to-body
      :close-on-click-modal="false"
      custom-class="student-dialog"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item label="学号" prop="number">
          <el-input v-model="form.number" placeholder="请输入学生学号" prefix-icon="el-icon-tickets" />
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" placeholder="请选择班级" style="width: 100%">
            <el-option
              v-for="item in classOptions"
              :key="item.classId"
              :label="item.className"
              :value="item.classId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent, getClassList } from "@/api/student/student";

export default {
  name: "Student",
  data() {
    return {
      classOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生信息管理表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        number: null,
        grade: null,
        className: null
      },
      // 表单参数
      form: {
        id: null,
        studentName: null,
        number: null,
        className: null
      },
      // 表单校验
      rules: {
        studentName: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        number: [
          { required: true, message: "学号不能为空", trigger: "blur" },
          { pattern: /^\d{8,12}$/, message: "学号格式不正确", trigger: "blur" }
        ],
        className: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getClassOptions();
  },
  methods: {
    /** 查询学生信息管理列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null,
        number: null,
        className: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 切换搜索框显示
    toggleSearch() {
      this.showSearch = !this.showSearch;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const studentId = row.id || row.studentId;
      getStudent(studentId).then(response => {
        this.form = {
          id: response.data.id,
          studentName: response.data.studentName,
          number: response.data.number,
          className: response.data.className
        };
        this.open = true;
        this.title = "修改学生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(error => {
              this.$modal.msgError("修改失败: " + error.message);
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(error => {
              this.$modal.msgError("新增失败: " + error.message);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.studentId;
      this.$modal.confirm('是否确认删除选中的学生信息？').then(function() {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 获取班级选项 */
    getClassOptions() {
      getClassList().then(response => {
        this.classOptions = response.data;
      }).catch(error => {
        this.$modal.msgError("获取班级选项失败: " + error.message);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
  
  .card-header-title {
    font-weight: bold;
    color: #409EFF;
    
    i {
      margin-right: 5px;
    }
  }
  
  .form-buttons {
    float: right;
  }
}

.operation-card {
  margin-bottom: 20px;
}

.table-card {
  .student-name {
    font-weight: 500;
    color: #409EFF;
  }
  
  .operation-btn {
    font-size: 16px;
    padding: 0 5px;
    
    &:hover {
      color: #409EFF;
    }
    
    &.delete-btn:hover {
      color: #F56C6C;
    }
  }
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

::v-deep .student-dialog {
  .el-dialog__header {
    border-bottom: 1px solid #eee;
  }
  
  .el-dialog__footer {
    border-top: 1px solid #eee;
  }
}
</style>